package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.dao.Area_banco;
import br.com.evl.tecinfo.dao.Usuario_Banco;
import br.com.evl.tecinfo.modelo.Area;
import br.com.evl.tecinfo.modelo.Usuario;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Tecinfo
 */
public class TelaCadastroUsuario extends javax.swing.JFrame {

    private Usuario novoUsuario;
    private Usuario_Banco usuarioBD;
    private Area_banco areaBD;

    private Area area;

    public TelaGeral telaGeral;
    int editar = 0;

    public TelaCadastroUsuario() {
        initComponents();

        novoUsuario = new Usuario();

        area = new Area();

        listarAreas();
        atualizarTabela();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        JT_Table = new javax.swing.JTable();
        jB_remover = new javax.swing.JButton();
        jB_editar1 = new javax.swing.JButton();
        jBcadastrar_tc1 = new javax.swing.JButton();
        JB_voltar = new javax.swing.JButton();
        jcbArea = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        JTF_nome = new javax.swing.JTextField();
        JTF_senha = new javax.swing.JTextField();
        JTF_cpf = new javax.swing.JTextField();
        JTF_login = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar usuário");
        setMaximumSize(new java.awt.Dimension(600, 600));
        setMinimumSize(new java.awt.Dimension(600, 600));
        setResizable(false);
        getContentPane().setLayout(null);

        JT_Table.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JT_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Senha", "CPF", "Login", "Área"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(JT_Table);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(40, 320, 510, 220);

        jB_remover.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_excluirv2.png"))); // NOI18N
        jB_remover.setText("Remover");
        jB_remover.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_removerActionPerformed(evt);
            }
        });
        getContentPane().add(jB_remover);
        jB_remover.setBounds(360, 550, 120, 40);

        jB_editar1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_editar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icon_editar1v2.png"))); // NOI18N
        jB_editar1.setText("Editar");
        jB_editar1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_editar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_editar1ActionPerformed(evt);
            }
        });
        getContentPane().add(jB_editar1);
        jB_editar1.setBounds(230, 550, 120, 40);

        jBcadastrar_tc1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBcadastrar_tc1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_add1v2.png"))); // NOI18N
        jBcadastrar_tc1.setText("Cadastrar");
        jBcadastrar_tc1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jBcadastrar_tc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcadastrar_tc1ActionPerformed(evt);
            }
        });
        getContentPane().add(jBcadastrar_tc1);
        jBcadastrar_tc1.setBounds(100, 550, 120, 40);

        JB_voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/go-back.png"))); // NOI18N
        JB_voltar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        JB_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_voltarActionPerformed(evt);
            }
        });
        getContentPane().add(JB_voltar);
        JB_voltar.setBounds(540, 10, 50, 40);

        jcbArea.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        getContentPane().add(jcbArea);
        jcbArea.setBounds(110, 280, 160, 24);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel9.setText("Área:");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(50, 280, 30, 15);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Senha:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(50, 160, 50, 20);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("CPF:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(50, 200, 50, 15);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Login:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(50, 240, 50, 20);

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 30)); // NOI18N
        jLabel2.setText("Cadastrar Usuário");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(190, 30, 260, 40);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Nome:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(50, 120, 50, 15);

        JTF_nome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTF_nome.setText("nome");
        getContentPane().add(JTF_nome);
        JTF_nome.setBounds(110, 110, 420, 30);

        JTF_senha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTF_senha.setText("00");
        JTF_senha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTF_senhaActionPerformed(evt);
            }
        });
        getContentPane().add(JTF_senha);
        JTF_senha.setBounds(110, 150, 420, 30);

        JTF_cpf.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTF_cpf.setText("000.000.000-00");
        JTF_cpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTF_cpfActionPerformed(evt);
            }
        });
        getContentPane().add(JTF_cpf);
        JTF_cpf.setBounds(110, 190, 420, 30);

        JTF_login.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTF_login.setText("login");
        JTF_login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTF_loginActionPerformed(evt);
            }
        });
        getContentPane().add(JTF_login);
        JTF_login.setBounds(110, 230, 420, 30);

        jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel1);
        jLabel1.setBounds(40, 100, 510, 210);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logomarcav2.png"))); // NOI18N
        getContentPane().add(jLabel8);
        jLabel8.setBounds(10, 0, 140, 110);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/back_azul.jpg"))); // NOI18N
        jLabel3.setMaximumSize(new java.awt.Dimension(600, 600));
        getContentPane().add(jLabel3);
        jLabel3.setBounds(0, 0, 600, 600);

        setSize(new java.awt.Dimension(616, 638));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void JTF_cpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_cpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JTF_cpfActionPerformed

    private void JTF_senhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_senhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JTF_senhaActionPerformed

    private void JTF_loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_loginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JTF_loginActionPerformed

    private void jB_removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_removerActionPerformed

        Usuario removeUsuario = new Usuario();

        removeUsuario.setCpf((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 2));

        usuarioBD.remover(removeUsuario);

        atualizarTabela();

    }//GEN-LAST:event_jB_removerActionPerformed

    private void jB_editar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_editar1ActionPerformed
        if (editar == 0) {

            JTF_nome.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 0));
            JTF_senha.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 1));
            JTF_cpf.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 2));
            JTF_login.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 3));
            jcbArea.setSelectedItem((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 4));
            jB_editar1.setText("Concluir");
            jB_remover.setEnabled(false);
            jBcadastrar_tc1.setEnabled(false);
            JT_Table.setEnabled(false);
            editar = 1;

        } else {

            jB_removerActionPerformed(evt);
            jBcadastrar_tc1ActionPerformed(evt);

            jB_editar1.setText("Editar");
            jB_remover.setEnabled(true);
            jBcadastrar_tc1.setEnabled(true);
            JT_Table.setEnabled(true);
            editar = 0;

        }


    }//GEN-LAST:event_jB_editar1ActionPerformed

    private void jBcadastrar_tc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcadastrar_tc1ActionPerformed

        Object item = jcbArea.getSelectedItem();

        areaBD = new Area_banco();

        try {
            while (areaBD.consulta.next()) {

                if (areaBD.consulta.getString("abreviacao").equals(item)) {
                    area.setNome(areaBD.consulta.getString("nome"));
                    area.setAbreviacao((String) item);
                }
            }
        } catch (SQLException ex) {
            System.out.println("erro ao criar objeto area");
        }

        novoUsuario.setNome(JTF_nome.getText());
        novoUsuario.setSenha(JTF_senha.getText());
        novoUsuario.setCpf(JTF_cpf.getText());
        novoUsuario.setLogin(JTF_login.getText());
        novoUsuario.setArea(area);
        usuarioBD.inserir(novoUsuario);

        atualizarTabela();

        //usuarioBD = new Usuario_Banco();
//        if (cadastrar_editar == -1) {
//            DefaultTableModel dtm = (DefaultTableModel) JT_Table.getModel();
//            Object[] opcoes = {JTF_nome.getText(), JTF_cpf.getText(),
//                JTF_senha.getText(), JTF_login.getText()};
//            dtm.addRow(opcoes);
//            JTF_nome.setText("");
//            JTF_cpf.setText("");
//            JTF_senha.setText("");
//            JTF_login.setText("");
//
//        } else {
//            int atualizar = JT_Table.getSelectedRow();
//            JT_Table.setValueAt(JTF_nome.getText(), atualizar, 0);
//            JT_Table.setValueAt(JTF_cpf.getText(), atualizar, 1);
//            JT_Table.setValueAt(JTF_senha.getText(), atualizar, 2);
//            JT_Table.setValueAt(JTF_login.getText(), atualizar, 3);
//
//            JOptionPane.showMessageDialog(null, "Dados atualizados com sucesso!");
//            cadastrar_editar = -1;
//            JTF_nome.setText("");
//            JTF_cpf.setText("");
//            JTF_senha.setText("");
//            JTF_login.setText("");
//        }
    }//GEN-LAST:event_jBcadastrar_tc1ActionPerformed

    private void JB_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_voltarActionPerformed

        this.setVisible(false);

    }//GEN-LAST:event_JB_voltarActionPerformed

    private void listarAreas() {

        areaBD = new Area_banco();

        try {
            while (areaBD.consulta.next()) {
                jcbArea.addItem(areaBD.consulta.getString("abreviacao"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void atualizarTabela() {

        usuarioBD = new Usuario_Banco();

        DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Nome", "Senha", "CPF", "Login", "Área"});

        try {
            while (usuarioBD.consulta.next()) {

                Object[] opcoes = {usuarioBD.consulta.getString("nome"), usuarioBD.consulta.getString("senha"), usuarioBD.consulta.getString("cpf"), usuarioBD.consulta.getString("login"), usuarioBD.consulta.getString("area")};
                dtm.addRow(opcoes);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroFormaPagamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        JT_Table.setModel(dtm);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_voltar;
    private javax.swing.JTextField JTF_cpf;
    private javax.swing.JTextField JTF_login;
    private javax.swing.JTextField JTF_nome;
    private javax.swing.JTextField JTF_senha;
    private javax.swing.JTable JT_Table;
    private javax.swing.JButton jB_editar1;
    private javax.swing.JButton jB_remover;
    private javax.swing.JButton jBcadastrar_tc1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbArea;
    // End of variables declaration//GEN-END:variables
}
